import random # 导入随机模块

# 定义成语列表和对应答案
idioms = ["十全十美", "一马当先", "百鸟朝凤", "百家争鸣", "魂不守舍",
          "鸡犬不宁", "五谷丰登", "三人成虎","首当其冲","首鼠两端"]
correct_answers = ['美', '马', '鸟', '家', '舍', '宁', '丰', '人','冲',"两"]
# 初始化一个空集合来跟踪已使用的成语索引
used = set()
# 定义函数来生成带有空白的成语
def create_idiom_with_blank(idiom, answer):
    idx = idiom.index(answer)
    new_idiom = idiom[:idx] + '_' + idiom[idx + 1:]
    return new_idiom
# 初始化分数
score = 20
# 游戏循环（只进行8关，确保不超过成语总数）
for level in range(1, 9):
    # 不断尝试直到找到一个未使用的成语索引
    while True:
        idiom_index = random.randint(0, len(idioms) - 1)
        if idiom_index not in used:
            used.add(idiom_index)
            break
    idiom = idioms[idiom_index]
    correct_answer = correct_answers[idiom_index]
    idiom_with_blank = create_idiom_with_blank(idiom, correct_answer)
    print(f"第{level}关: {idiom_with_blank}（请填写缺失的字）")
    user_answer = input("请输入你的答案: ").strip()
    # 检查用户答案是否正确
    if user_answer == correct_answer:
        print("答对了，你真棒！")
        score += 5
    elif user_answer != correct_answer:
        print(f"错了，正确答案是: {correct_answer}。请再试一次。")
        score -= 5
    elif user_answer == "过":
        print("跳过此关")

# 输出成绩
print(f"游戏结束，你的最终成绩是: {score}分")